package com.hwh.blog.web.admin;

import com.hwh.blog.po.User;
import com.hwh.blog.service.UserService;
import com.hwh.blog.service.UserServiceImpl;
import com.hwh.blog.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private UserServiceImpl userService;
    @GetMapping
    public String LoginPage(){
        return "admin/login";
    }
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session, RedirectAttributes attributes){
        User user = userService.checkUser(username, MD5Util.getPwd(password));
        if (user!=null)
        {
            user.setPassword(null);
            session.setAttribute("user",user);
            return "admin/index";
        } else {
            attributes.addFlashAttribute("message","用户名密码错误");
            return "redirect:admin/login";
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession s){
        s.removeAttribute("user");
        return "redirect:/admin";
    }
    @GetMapping("/index")
    public String GotoIndex(HttpSession s){
        System.out.println(s.getAttribute("user"));
        return "admin/index";
    }
}
